#!/bin/bash

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' # 无颜色

# 日志函数
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查是否为root用户
if [ "$(id -u)" != "0" ]; then
   log_error "此脚本必须以root权限运行" 
   exit 1
fi

# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# 获取项目根目录
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

log_info "开始部署妙趣拾光锦盒管理系统..."
log_info "项目根目录: $PROJECT_ROOT"

# 安装系统依赖
log_info "正在安装系统依赖..."
yum update -y
yum install -y python3 python3-devel mysql-devel gcc nginx supervisor git

# 创建应用目录
APP_DIR="/var/www/box-admin"
log_info "创建应用目录: $APP_DIR"
mkdir -p $APP_DIR
mkdir -p /var/log/box-admin

# 复制项目文件到应用目录
log_info "复制项目文件到应用目录..."
cp -r $PROJECT_ROOT/* $APP_DIR/

# 创建并激活虚拟环境
log_info "创建虚拟环境..."
cd $APP_DIR
python3 -m venv venv
source venv/bin/activate

# 安装Python依赖
log_info "安装Python依赖..."
pip install --upgrade pip
pip install -r requirements.txt
pip install gunicorn

# 创建启动脚本
log_info "创建启动脚本..."
cat > $APP_DIR/start.sh << 'EOF'
#!/bin/bash
cd /var/www/box-admin
source venv/bin/activate
export FLASK_ENV=production
export SECRET_KEY="please-change-me-to-a-random-secret-key"
export DATABASE_URL="mysql+pymysql://csss:12345@106.15.51.42:3306/csss"
exec gunicorn --workers=4 --bind=127.0.0.1:5000 --timeout=120 app:app
EOF

chmod +x $APP_DIR/start.sh

# 配置Supervisor
log_info "配置Supervisor..."
cat > /etc/supervisord.d/box-admin.ini << 'EOF'
[program:box-admin]
command=/var/www/box-admin/start.sh
directory=/var/www/box-admin
user=root
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/box-admin/supervisor.err.log
stdout_logfile=/var/log/box-admin/supervisor.out.log
EOF

# 配置Nginx
log_info "配置Nginx..."
cat > /etc/nginx/conf.d/box-admin.conf << 'EOF'
server {
    listen 80;
    server_name _;  # 修改为您的域名

    access_log /var/log/nginx/box-admin.access.log;
    error_log /var/log/nginx/box-admin.error.log;

    location /static/ {
        alias /var/www/box-admin/app/static/;
        expires 30d;
    }

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_connect_timeout 300s;
        proxy_read_timeout 300s;
    }
}
EOF

# 创建logs目录
log_info "创建日志目录..."
mkdir -p $APP_DIR/logs

# 设置文件权限
log_info "设置文件权限..."
chown -R root:root $APP_DIR
chmod -R 755 $APP_DIR
chown -R root:root /var/log/box-admin

# 启用并启动服务
log_info "启动服务..."
systemctl enable supervisord
systemctl enable nginx
systemctl restart supervisord
systemctl restart nginx

# 配置防火墙
log_info "配置防火墙..."
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

log_info "部署完成!"
log_info "您现在可以通过服务器IP或域名访问管理系统。"
log_info "如需自定义域名，请修改 /etc/nginx/conf.d/box-admin.conf 文件的server_name字段。"
log_info "请确保修改启动脚本中的SECRET_KEY为随机字符串以增强安全性。"

# 检查服务状态
log_info "服务状态检查:"
echo "----------------- Supervisor 状态 -----------------"
supervisorctl status box-admin
echo "----------------- Nginx 状态 -----------------"
systemctl status nginx | grep "Active:" 